#include "telephone.h"
#include "DynamicArray.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

TInfo *CreateTBook(const char *name, const char *num)
{
    TInfo *book = (TInfo *)malloc(sizeof(TInfo));
    if (book == NULL)
    {
        printf("CreateTInfo malloc error!\n");
        exit(-1);
    }
    book->name = strdup(name);
    book->num = strdup(num);
    return book;
}

void output_user(TInfo *a, int index)
{
    printf("%d ", index + 1);
    printf("联系人姓名：%s   ", a->name);
    printf("联系人号码：%s\n", a->num);
}

bool SmallerName(void *p1, void *p2)
{
    TInfo *s1 = (TInfo *)p1;
    TInfo *s2 = (TInfo *)p2;
    return strcmp(s1->name, s2->name) < 0;
}

void ShowAll(DArray *a)
{
    if (GetSize(a) == 0)
        printf("通讯录没有用户信息！\n");
    else
        for (int i = 0; i < GetSize(a); i++)
        {
            TInfo *current_user = (TInfo *)(GetArray(a)[i]);
            output_user(current_user, i);
        }
}

bool IsNameEqual(void *p1, void *p2)
{
    TInfo *book = (TInfo *)p1;
    const char *name = (const char *)p2;
    return strcmp(book->name, name) == 0;
}

bool IsNumEqual(void *p1, void *p2)
{
    TInfo *book = (TInfo *)p1;
    const char *num = (const char *)p2;
    return strcmp(book->num, num) == 0;
}

void InsertTelephoneBook(DArray *a)
{
    char name[Size];
    char num[Size];
    printf("请输入所需要添加的联系人:");
    scanf("%s", name);
    printf("请输入联系人电话号码:");
    scanf("%s", num);
    InsertDArray(a, CreateTBook(name, num));
    FastSort(a, SmallerName);
    printf("添加成功!\n");
}

void DeleteTelephoneBook(DArray *a)
{
    printf("请输入需要删除的联系人电话号码:");
    char Num[Size];
    scanf("%s", Num);
    void *s = RemoveDArray(a, Num, IsNumEqual);
    if (s)
    {
        printf("删除的是:\n");
        PrintTBook(s);
        ClearTBook(s);
    }
    else
    {
        printf("查无此号码\n");
    }
}

void SearchByInfo(DArray *a)
{
    int choice = 0;
    printf("请选择你想选择的功能: 1、搜索联系人    2、根据姓或名进行查找\n");
    scanf("%d", &choice);
    char info[Size];
    printf("请输入用户名进行搜索:\n");
    scanf("%s", info);
    if (choice == 1)
    {
        FindAllByElement(a, info, IsNameEqual, PrintTBook);
    }
    else if (choice == 2)
    {
        FuzzySearch(a, info);
    }
}

void IsUpdateTBook(void *p)
{
    TInfo *book = (TInfo *)p;
    printf("请输入修改后的联系人号码:");
    scanf("%s", book->name);
}

void UpdateTBook(void *p)
{
    TInfo *book = (TInfo *)p;
    printf("请输入修改后的联系人号码:");
    scanf("%s", book->num);
    printf("电话号码更新成功！\n");
}

void UpdateTelephoneBook(DArray *a)
{
    char Name[Size];
    char Num[Size];
    printf("请输入需要修改的联系人电话号码:\n");
    scanf("%s", Num);

    // 找到匹配的联系人
    TInfo *book = FindByElement(a, Num, IsNumEqual);
    if (book != NULL)
    {
        printf("找到联系人：%s   号码：%s\n", book->name, book->num);
        printf("请输入新的电话号码:\n");
        scanf("%s", book->num);
        printf("电话号码更新成功！\n");
        UpdateDArray(a, book, IsNumEqual, UpdateTBook);
    }
    else
    {
        printf("没有找到匹配的联系人。\n");
    }
}

void PrintTBook(void *p)
{
    TInfo *book = (TInfo *)p;
    printf("联系人:%s   号码:%s\n", book->name, book->num);
}

void FuzzySearch(DArray *a, const char *prefix)
{
    printf("模糊查询结果（以 '%s' 开头的用户）:\n", prefix);
    for (int i = 0; i < GetSize(a); i++)
    {
        TInfo *current_user = (TInfo *)GetArray(a)[i];
        if (strstr(current_user->name, prefix) != NULL)
        {
            output_user(current_user, i);
        }
    }
    printf("\n");
}

void ClearTBook(void *p)
{
    TInfo *book = (TInfo *)p;
    free(book->name);
    free(book->num);
    free(book);
}
